package com.zmj.sy.mom.srv.aps.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.dto.staff.StaffDto;
import com.zmj.sy.mom.srv.aps.bean.dto.staff.StaffSelectDto;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.PhaseBoss;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BasePageResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseSelectItemResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.phase.*;
import com.zmj.sy.mom.srv.aps.feign.StaffApiClient;
import com.zmj.sy.mom.srv.aps.mapper.PhaseBossMapper;
import com.zmj.sy.mom.srv.aps.utils.MkBeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class PhaseBossService {

    private final PhaseBossMapper phaseBossMapper;
    private final StaffApiClient staffApiClient;


    public BasePageResVo<PhaseBossSelectResVo> bossList(PhaseBossSelectReqVo reqVo) {
        LambdaQueryWrapper<PhaseBoss> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(reqVo.getPhaseCode()), PhaseBoss::getPhaseCode, reqVo.getPhaseCode());
        queryWrapper.orderByDesc(PhaseBoss::getId);
        Page<PhaseBoss> page = phaseBossMapper.selectPage(reqVo.toPage(), queryWrapper);

        return BasePageResVo.of(page, PhaseBossSelectResVo.class);
    }

    @Transactional
    public void bossAdd(PhaseBossAddReqVo reqVo) {
        PhaseBoss phaseBoss = MkBeanUtils.copyProperties(reqVo, PhaseBoss.class);
        phaseBossMapper.insert(phaseBoss);
    }

    @Transactional
    public void bossEdit(PhaseBossEditReqVo reqVo) {
        PhaseBoss phase = MkBeanUtils.copyProperties(reqVo, PhaseBoss.class);

        phaseBossMapper.updateById(phase);
    }

    @Transactional
    public void bossDelete(BaseIdReqVo reqVo) {
        phaseBossMapper.deleteById(reqVo.getId());
    }

    @Transactional
    public List<BaseSelectItemResVo> bossSelect(PhaseBossSelect2ResVo reqVo) {
        StaffSelectDto dto = new StaffSelectDto();
        BeanUtils.copyProperties(reqVo, dto);
        List<StaffDto> staffDtoList = staffApiClient.getStaffList(dto).getData();
        System.out.println(staffDtoList);

        return staffDtoList.stream()
                .map(e->{
                    BaseSelectItemResVo item = new BaseSelectItemResVo();
                    item.setText(e.getStaffCode() + "-" +  e.getStaffName());
                    item.setValue(e.getStaffCode());
                    return item;

                })
                .collect(Collectors.toList());
    }

}
